package com.pkdl.dao;

import com.pkdl.dto.DichVuDTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

// Data access object, which get data of DichVu table from database
public class DichVuDAO {
	public boolean delete(int madichvu) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_DichVu_Delete(?)}");
			cs.setInt(1, madichvu);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

	public boolean insert(DichVuDTO dichvu) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_DichVu_Insert(?, ?, ?)}");
			cs.setString(1, dichvu.getTenDichVu());
			cs.setLong(2, dichvu.getPhiDichVu());
			cs.setString(3, dichvu.getMoTa());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

	public ArrayList<DichVuDTO> selectAll() {
		ArrayList<DichVuDTO> list = new ArrayList<DichVuDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_DichVu_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				DichVuDTO dichvu = new DichVuDTO();
				dichvu.setMaDichVu(rs.getInt("MaDichVu"));
				dichvu.setTenDichVu(rs.getString("TenDichVu"));
				dichvu.setPhiDichVu(rs.getLong("PhiDichVu"));
				dichvu.setMoTa(rs.getString("MoTa"));
				list.add(dichvu);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return list;
	}

	public DichVuDTO selectById(int madichvu) {
		DichVuDTO dichvu = null;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_DichVu_SelectByID(?)}");
			cs.setInt(1, madichvu);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				dichvu = new DichVuDTO();
				dichvu.setMaDichVu(rs.getInt("MaDichVu"));
				dichvu.setTenDichVu(rs.getString("TenDichVu"));
				dichvu.setPhiDichVu(rs.getLong("PhiDichVu"));
				dichvu.setMoTa(rs.getString("MoTa"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return dichvu;
	}

	public boolean update(DichVuDTO dichvu) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_DichVu_Update(?, ?, ?, ?)}");
			cs.setInt(1, dichvu.getMaDichVu());
			cs.setString(2, dichvu.getTenDichVu());
			cs.setLong(3, dichvu.getPhiDichVu());
			cs.setString(4, dichvu.getMoTa());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(DichVuDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

}
